package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.bean.ApiResult;
import com.example.mapper.CommentMapper;
import com.example.mapper.PostMapper;
import com.example.modules.dto.PostDTO;
import com.example.modules.entity.Comment;
import com.example.modules.entity.Post;
import com.example.modules.vo.MyPostVO;
import com.example.modules.vo.PostVO;
import com.example.service.CommentService;
import com.example.service.PostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PostImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentService commentService;
    @Autowired
    private PostMapper postMapper;

    @Override
    public ApiResult insertPost(PostDTO postDTO) {
        Post post = new Post();
        BeanUtils.copyProperties(postDTO,post);
        post.setAuditStatus(0);
        post.setPublishTime(new Date());
        save(post);
        return ApiResult.success();
    }

    @Override
    public ApiResult detailPost(Integer id) {
        Post post = getById(id);
        PostVO postVO = PostVO.of(post);
        BeanUtils.copyProperties(post,postVO);
        //查询该贴的评论数
        postVO.setCommentNum(commentMapper.getCommentNum(id));
        return ApiResult.success(postVO);
    }

    @Override
    public ApiResult deletePostByPostId(Integer postId) {
        Post post = getById(postId);
        if (Objects.nonNull(post)){
            //删除帖子
            post.setIsDelete(1);
            post.setUpdateTime(new Date());
            updateById(post);
            //删除该帖子的所有评论
           commentMapper.delCommentByPostId(postId);
        }
        return ApiResult.success();
    }

    @Override
    public ApiResult getMyPost(Integer userId) {
        QueryWrapper<Post> qw = new QueryWrapper<>();
        qw.eq("user_id",userId).eq("is_delete",0).orderByDesc("publish_time");
        List<PostVO> postVOList = list(qw).stream().map(PostVO::of).collect(Collectors.toList());
        postVOList.forEach(item->{
            item.setCommentNum(commentMapper.getCommentNum(item.getId()));
        });
        return ApiResult.success(postVOList);
    }

    @Override
    public ApiResult getMyCommentPost(Integer userId) {
        List<MyPostVO> postVOList = postMapper.getMyCommentPost(userId);
        postVOList.forEach(item->{
            item.setCommentNum(commentMapper.getCommentNum(item.getId()));
        });
        return ApiResult.success(postVOList);
    }

    @Override
    public ApiResult getMyPostComment(Integer userId) {
        return ApiResult.success(commentMapper.getMyPostComment(userId));
    }
}
